var express = require('express');
var router = express.Router();
var goodsController = require("../controller/goodsController")



//获取所有食品信息
router.get("/getAll", async (req, res, next) => {
    var data = await goodsController.getall()
    res.json({
        code: 1,
        msg: "获取所有食品信息成功",
        data: data
    })
});

//获取单个商铺数据
router.get("/get/:goodsname",async (req,res,next)=>{
    var params = req.params
    var data = await goodsController.query(params)
    res.json({
        code:1,
        msg:"获取成功",
        data
    })
})

router.get("/get",async (req,res,next)=>{
    var params = req.query
    var data = await goodsController.query(params)
    res.json({
        code:1,
        msg:"获取成功",
        data
    })
})

//添加食品信息
router.post("/add",(req, res, next) => {
    let body = req.body;
    let insert = goodsController.add(body)
    res.json({
        code: 1,
        msg: "添加食品信息成功",
        insert
    })
})

//修改食品信息接口
router.put("/modify/:_id",async (req, res, next) => {
    let data = req.body;
    let params = req.params;
    let result = await goodsController.update(data,params)
    if(result.n>=1){
        res.json({
            code: 1,
            msg: "更新成功",
        })
    }else{
        res.json({
            code: 1,
            msg: "没有更新任何数据",
            result
        })
    }
})

//删除食品信息
router.delete("/delete/:_id",async (req,res,next)=>{
    let params = req.params;
    let result = await goodsController.delete(params)
    if(result.n>=1){
        res.json({
            code:1,
            msg:"删除成功",
        })
    }else{
        res.json({
            code:0,
            msg:"删除失败",
        })
    }
});



module.exports = router;